{% extends 'base_user_center.html' %}
{% load static %}
{% block right_content %}
    <div class="right_content clearfix">
        <h3 class="common_title2">收货地址</h3>
        <div class="site_con">
            <dl>
                <dt>当前地址：</dt>
                {% if address %}
                    {% for item in address %}
                        <dd>
                            {{ item.addr }} （{{ item.receiver }} 收） {{ item.phone }}
                            <div class="user_address_handle">
                                <span onclick="userAddressModify({'id':{{ item.id }},'receiver':'{{item.receiver}}','zip_code':'{{ item.zip_code }}','phone': '{{ item.phone }}','addr':'{{ item.addr }}' })">修改</span>
                                <span onclick="userAddressDelete({{ user.id }}, {{ item.id }})">删除</span>
                                {% if item.is_default %}
                                    <span class="default t-needUpdate">默认地址</span>
                                {% else %}
                                    <span onclick="userAddressDefualt({{ user.id }}, {{ item.id }})">设置默认</span>
                                {% endif %}
                            </div>
                        </dd>
                    {% endfor %}
                {% else %}
                    <dd>无默认地址</dd>
                {% endif %}

            </dl>
        </div>
        <h3 class="common_title2">编辑地址</h3>
        <div class="site_con">
            <form method="post">
                {% csrf_token %}
                <div class="form_group">
                    <label>收件人：</label>
                    <input type="text" name="receiver" id="receiver">
                </div>
                <div class="form_group form_group2">
                    <label>详细地址：</label>
                    <textarea class="site_area" name="addr" id="addr"></textarea>
                </div>
                <div class="form_group">
                    <label>邮编：</label>
                    <input type="text" name="zip_code" id="zip_code">
                </div>
                <div class="form_group">
                    <label>手机：</label>
                    <input type="text" name="phone" maxlength="11" id="phone">
                </div>

                <input type="submit" name="" value="提交" class="info_submit">
            </form>
        </div>
    </div>
{% endblock right_content %}

{% block bottomfile %}
    <script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
    <script type="text/javascript">
        var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();

        function userAddressModify(obj) {
            $('#receiver').val(obj.receiver);
            $('#addr').val(obj.addr);
            $('#phone').val(obj.phone);
            $('#zip_code').val(obj.zip_code);
            $('form').prepend("<input name='id' value='"+obj.id+"' type='hidden'>");
        }

        function userAddressDelete(userId, addressId) {
            $.ajax({
                type: 'patch',
                url: '/user/address',
                data: {
                    userId: userId,
                    addressId: addressId,
                    code: 'delete',
                },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                },
                success: function (data) {
                    if (data.code == 0) {
                        window.location.reload()
                    }
                }
            })
        }

        function userAddressDefualt(userId, addressId) {
            $.ajax({
                type: 'patch',
                url: '/user/address',
                data: {
                    userId: userId,
                    addressId: addressId,
                    code: 'address',
                },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                },
                success: function (data) {
                    if (data.code == 0) {
                        window.location.reload()
                    }
                }
            })
        }
    </script>
{% endblock bottomfile %}
